/**
 * 页面初始化完成回调
 */
myApp_.onPageInit('seller_order_list', function (page) {
    //初始化分页数据
    SELLER_ORDER_LIST.page = 0;
    SELLER_ORDER_LIST.last_page = 1;

	//请求商家订单状态值列表
    SELLER_ORDER_LIST.get_status();
    //请求数据
    SELLER_ORDER_LIST.request();
//  SELLER_ORDER_LIST.fill_status();
});


/**
 * LIST 对象 
 */
SELLER_ORDER_LIST = {}
//订单状态值
SELLER_ORDER_LIST.ORDER_STATUS_DATA = '';
//订单页面参数
SELLER_ORDER_LIST.param = {};

//页面翻页数据
SELLER_ORDER_LIST.page = 0;
SELLER_ORDER_LIST.last_page = 1;
SELLER_ORDER_LIST.param.order_status_id = '';

//翻页启动装置
SELLER_ORDER_LIST.scroll_flag = true;

//页面数据
SELLER_ORDER_LIST.data = {};


SELLER_ORDER_LIST.__INIT__ = function (status) {
	if(!status&&status!=0){
		status = 0;
	}
    SELLER_ORDER_LIST.param.order_status_id = status;
    
    mainView.router.load({
        url: "src/seller/order/list.html"
    });
}

/**
 * 翻页
 */
SELLER_ORDER_LIST.scroll = function (e) {
    var me = $(e);
    var viewH = me.height();
    var contentH = me.get(0).scrollHeight;
    var scrollTop = me.scrollTop();
    if (scrollTop / (contentH - viewH) >= 0.95) {
        if (this.scroll_flag) {
            this.scroll_flag = false;
        } else {
            return;
        }
        this.request();
    }
}

/**
 * 请求数据
 */
SELLER_ORDER_LIST.request = function () {
    if (SELLER_ORDER_LIST.page >= SELLER_ORDER_LIST.last_page) {
        return false;
    }
    //加载下一页
    SELLER_ORDER_LIST.page++;
    SELLER_ORDER_LIST.param.page = SELLER_ORDER_LIST.page;
    request_(
        url_('addons/execute/seller-App-get_seller_order')
        , SELLER_ORDER_LIST.param
        , function (d) {
            if (d.code == 0) {
				SELLER_ORDER_LIST.page = d.data.order_data.current_page;
                SELLER_ORDER_LIST.last_page = d.data.order_data.last_page;
                
                SELLER_ORDER_LIST.data = d.data.order_data;
                SELLER_ORDER_LIST.fill_list();
            } else {
                my_tips_(d.msg);
            }
        }
    );
}

/**
 * 状态筛选栏
 */
//获取订单状态表
SELLER_ORDER_LIST.get_status = function(){
	request_(
		url_('addons/execute/seller-App-get_seller_order_status')
		,{}
		,function(d){
			if (d.code == 0) {
				if(!SELLER_ORDER_LIST.ORDER_STATUS_DATA){
					SELLER_ORDER_LIST.ORDER_STATUS_DATA = d.data;
				}
				SELLER_ORDER_LIST.fill_status();
            } else {
                my_tips_(d.msg);
            }
		}
	);
}

SELLER_ORDER_LIST.fill_status = function(){
	tpl_fill(
		SELLER_ORDER_LIST.status_tpl
		,SELLER_ORDER_LIST.ORDER_STATUS_DATA
		,'.seller_order_status_content'
	);
	
	var status = SELLER_ORDER_LIST.param.order_status_id;
	$(".select_list .tab-link").removeClass("active");
    $(".select_list .tab-link[status='"+status+"'").addClass("active");
}

SELLER_ORDER_LIST.status_tpl = '' +
    '{{#each this}}' +
    '<a onclick="SELLER_ORDER_LIST.set_status({{this.ordr_status_id}})" status="{{ordr_status_id}}" class="tab-link mypay" style="display: inline-block;width: 20%;float: none;height: 2.8rem;">{{title}}</a>' +
    '{{/each}}';

/**
 * 订单状态筛选
 * @param {Object} status
 */
SELLER_ORDER_LIST.set_status = function (status) {
	//初始化翻页数据
    SELLER_ORDER_LIST.page = 0;
    SELLER_ORDER_LIST.last_page = 1;
    //订单状态
    SELLER_ORDER_LIST.param.order_status_id = status;
    //清空当前数据 请求新数据
    $('#seller_order_container').html('');
    SELLER_ORDER_LIST.request();
	//标签页active渲染
    $(".select_list .tab-link").removeClass("active");
    $(".select_list .tab-link[status='"+status+"'").addClass("active");
}

//传入状态code  返回状态
SELLER_ORDER_LIST.order_status = function (e) {
	var title = '';
	var status_list = SELLER_ORDER_LIST.ORDER_STATUS_DATA;
	status_list.forEach(function(v,i,arr){
		if(v.ordr_status_id == e){
			return title = v.title;
		}
	});
	return title;
};

/**
 * 订单列表
 */
SELLER_ORDER_LIST.fill_list = function () {
    $.tpl({
        url: 'src/seller/order/list_tpl.html'
        , context: SELLER_ORDER_LIST.data.data
        , elem: '#seller_order_container'
        , append: true
        , before: function (param) {
            if (SELLER_ORDER_LIST.page >= SELLER_ORDER_LIST.last_page) {
                param.html += '<p class="daodile_" style="text-align: center;">没有更多订单</p>'
            }
            return param;
        }
        , after: function () {
            //scroll_flag 封印解除
            SELLER_ORDER_LIST.scroll_flag = true;
        }
    });
}

//SELLER_ORDER_LIST.detail = function(){
//	var order_id = 1234565;
//	go_('order_detail/'+order_id);
//}

////确认发货操作
//SELLER_ORDER_LIST.confirm_send = function(oid){
//	myApp_.confirm("确认已经发货？",function(){
//		request_(
//    		url_('addons/execute/seller-App-update_o_status')
//    		,{
//    			order_id: oid
//    		}
//    		,function(d){
//    			if (d.code == 0) {
//    			 	my_tips_('确认发货成功',function(){
//    			 		//刷新当前状态订单列表
//					    $('#seller_order_container').html('');
//					    SELLER_ORDER_LIST.page = 0;
//					    SELLER_ORDER_LIST.last_page = 1;
//					    SELLER_ORDER_LIST.request();
//    			 	});
//	            } else {
//	                my_tips_(d.msg);
//	            }
//    		}
//    	);
//	});
//}
//


//确认接单操作
SELLER_ORDER_LIST.accept_order = function(oid){
	myApp_.confirm("确认接单？",function(){
		request_(
				url_('addons/execute/seller-App-accept_order')
				,{
					order_id: oid
				}
				,function(d){
					if (d.code == 0) {
						my_tips_('接单成功',function(){
							//刷新当前状态订单列表
							$('#seller_order_container').html('');
							SELLER_ORDER_LIST.page = 0;
							SELLER_ORDER_LIST.last_page = 1;
							SELLER_ORDER_LIST.request();
						});
					} else {
						my_tips_(d.msg);
					}
				}
		);
	});
}



//确认配送成功操作
SELLER_ORDER_LIST.querenshouhuo = function(oid){
	myApp_.confirm("确认配送？",function(){
		request_(
				url_('addons/execute/seller-App-querenshouhuo')
				,{
					order_id: oid
				}
				,function(d){
					if (d.code == 0) {
						my_tips_('配送成功',function(){
							//刷新当前状态订单列表
							$('#seller_order_container').html('');
							SELLER_ORDER_LIST.page = 0;
							SELLER_ORDER_LIST.last_page = 1;
							SELLER_ORDER_LIST.request();
						});
					} else {
						my_tips_(d.msg);
					}
				}
		);
	});
}


